﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Threading;
using System.Windows.Forms;
using HslCommunication.Core;

namespace HslCommunication.Controls
{
	/// <summary>
	/// 仪表盘控件类
	/// </summary>
	// Token: 0x02000175 RID: 373
	public class UserGaugeChart : UserControl
	{
		/// <summary>
		/// 实例化一个仪表盘控件
		/// </summary>
		// Token: 0x06001EC1 RID: 7873 RVA: 0x000945F8 File Offset: 0x000927F8
		public UserGaugeChart()
		{
			this.InitializeComponent();
			this.pen_gauge_border = new Pen(this.color_gauge_border);
			this.brush_gauge_pointer = new SolidBrush(this.color_gauge_pointer);
			this.centerFormat = new StringFormat();
			this.centerFormat.Alignment = StringAlignment.Center;
			this.centerFormat.LineAlignment = StringAlignment.Center;
			this.pen_gauge_alarm = new Pen(Color.OrangeRed, 3f);
			this.pen_gauge_alarm.DashStyle = DashStyle.Custom;
			this.pen_gauge_alarm.DashPattern = new float[]
			{
				5f,
				1f
			};
			this.hybirdLock = new SimpleHybirdLock();
			this.m_UpdateAction = new Action(base.Invalidate);
			this.timer_alarm_check = new Timer();
			this.timer_alarm_check.Tick += this.Timer_alarm_check_Tick;
			this.timer_alarm_check.Interval = 1000;
			this.DoubleBuffered = true;
		}

		// Token: 0x06001EC2 RID: 7874 RVA: 0x000947BC File Offset: 0x000929BC
		private void Timer_alarm_check_Tick(object sender, EventArgs e)
		{
			bool flag = this.value_current > this.value_alarm_max || this.value_current < this.value_alarm_min;
			if (flag)
			{
				this.alarm_check = !this.alarm_check;
			}
			else
			{
				this.alarm_check = false;
			}
			base.Invalidate();
		}

		// Token: 0x06001EC3 RID: 7875 RVA: 0x00094810 File Offset: 0x00092A10
		private void UserGaugeChart_Load(object sender, EventArgs e)
		{
			this.timer_alarm_check.Start();
		}

		// Token: 0x06001EC4 RID: 7876 RVA: 0x00094820 File Offset: 0x00092A20
		private void UserGaugeChart_Paint(object sender, PaintEventArgs e)
		{
			bool flag = !Authorization.nzugaydgwadawdibbas();
			if (!flag)
			{
				Graphics graphics = e.Graphics;
				graphics.SmoothingMode = SmoothingMode.HighQuality;
				graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
				OperateResult<Point, int, double> centerPoint = this.GetCenterPoint();
				bool flag2 = !centerPoint.IsSuccess;
				if (!flag2)
				{
					Point content = centerPoint.Content1;
					int content2 = centerPoint.Content2;
					float num = Convert.ToSingle(centerPoint.Content3);
					Rectangle rect = new Rectangle(-content2, -content2, 2 * content2, 2 * content2);
					Rectangle rect2 = new Rectangle(-content2 - 5, -content2 - 5, 2 * content2 + 10, 2 * content2 + 10);
					Rectangle rect3 = new Rectangle(-content2 / 3, -content2 / 3, 2 * content2 / 3, 2 * content2 / 3);
					graphics.TranslateTransform((float)content.X, (float)content.Y);
					graphics.DrawArc(this.pen_gauge_border, rect3, -num, num * 2f - 180f);
					graphics.DrawArc(this.pen_gauge_border, rect, num - 180f, 180f - num * 2f);
					graphics.DrawLine(this.pen_gauge_border, (int)((double)(-(double)(content2 / 3)) * Math.Cos((double)(num / 180f) * 3.141592653589793)), -(int)((double)(content2 / 3) * Math.Sin((double)(num / 180f) * 3.141592653589793)), -(int)((double)(content2 - 30) * Math.Cos((double)(num / 180f) * 3.141592653589793)), -(int)((double)(content2 - 30) * Math.Sin((double)(num / 180f) * 3.141592653589793)));
					graphics.DrawLine(this.pen_gauge_border, (int)((double)(content2 - 30) * Math.Cos((double)(num / 180f) * 3.141592653589793)), -(int)((double)(content2 - 30) * Math.Sin((double)(num / 180f) * 3.141592653589793)), (int)((double)(content2 / 3) * Math.Cos((double)(num / 180f) * 3.141592653589793)), -(int)((double)(content2 / 3) * Math.Sin((double)(num / 180f) * 3.141592653589793)));
					graphics.RotateTransform(num - 90f);
					int num2 = this.segment_count;
					for (int i = 0; i <= num2; i++)
					{
						Rectangle rectangle = new Rectangle(-2, -content2, 3, 7);
						graphics.FillRectangle(Brushes.DimGray, rectangle);
						rectangle = new Rectangle(-50, -content2 + 7, 100, 20);
						graphics.DrawString((this.ValueStart + (this.ValueMax - this.ValueStart) * (double)i / (double)num2).ToString(), this.Font, Brushes.DodgerBlue, rectangle, this.centerFormat);
						graphics.RotateTransform((180f - 2f * num) / (float)num2 / 2f);
						bool flag3 = i != num2;
						if (flag3)
						{
							graphics.DrawLine(Pens.DimGray, 0, -content2, 0, -content2 + 3);
						}
						graphics.RotateTransform((180f - 2f * num) / (float)num2 / 2f);
					}
					graphics.RotateTransform(-(180f - 2f * num) / (float)num2);
					graphics.RotateTransform(num - 90f);
					Rectangle rectangle2 = new Rectangle(-36, -(content2 * 2 / 3 - 3), 72, this.Font.Height + 3);
					bool flag4 = this.value_current > this.value_alarm_max || this.value_current < this.value_alarm_min;
					if (flag4)
					{
						bool flag5 = this.alarm_check;
						if (flag5)
						{
							graphics.FillRectangle(Brushes.OrangeRed, rectangle2);
						}
					}
					bool isTextUnderPointer = this.IsTextUnderPointer;
					if (isTextUnderPointer)
					{
						graphics.DrawString(this.Value.ToString(), this.Font, Brushes.DimGray, rectangle2, this.centerFormat);
						rectangle2.Offset(0, this.Font.Height);
						bool flag6 = !string.IsNullOrEmpty(this.UnitText);
						if (flag6)
						{
							graphics.DrawString(this.UnitText, this.Font, Brushes.Gray, rectangle2, this.centerFormat);
						}
					}
					graphics.RotateTransform(num - 90f);
					graphics.RotateTransform((float)((this.value_paint - this.ValueStart) / (this.ValueMax - this.ValueStart) * (double)(180f - 2f * num)));
					Rectangle rect4 = new Rectangle(-5, -5, 10, 10);
					graphics.FillEllipse(this.brush_gauge_pointer, rect4);
					Point[] points = new Point[]
					{
						new Point(5, 0),
						new Point(2, -content2 + 40),
						new Point(0, -content2 + 20),
						new Point(-2, -content2 + 40),
						new Point(-5, 0)
					};
					graphics.FillPolygon(this.brush_gauge_pointer, points);
					graphics.RotateTransform((float)(-(float)(this.value_paint - this.ValueStart) / (this.ValueMax - this.ValueStart) * (double)(180f - 2f * num)));
					graphics.RotateTransform(90f - num);
					bool flag7 = this.value_alarm_min > this.ValueStart && this.value_alarm_min <= this.ValueMax;
					if (flag7)
					{
						graphics.DrawArc(this.pen_gauge_alarm, rect2, num - 180f, (float)((this.ValueAlarmMin - this.ValueStart) / (this.ValueMax - this.ValueStart) * (double)(180f - 2f * num)));
					}
					bool flag8 = this.value_alarm_max >= this.ValueStart && this.value_alarm_max < this.ValueMax;
					if (flag8)
					{
						float num3 = (float)((this.value_alarm_max - this.ValueStart) / (this.ValueMax - this.ValueStart) * (double)(180f - 2f * num));
						graphics.DrawArc(this.pen_gauge_alarm, rect2, -180f + num + num3, 180f - 2f * num - num3);
					}
					bool flag9 = !this.IsTextUnderPointer;
					if (flag9)
					{
						graphics.DrawString(this.Value.ToString(), this.Font, Brushes.DimGray, rectangle2, this.centerFormat);
						rectangle2.Offset(0, this.Font.Height);
						bool flag10 = !string.IsNullOrEmpty(this.UnitText);
						if (flag10)
						{
							graphics.DrawString(this.UnitText, this.Font, Brushes.Gray, rectangle2, this.centerFormat);
						}
					}
					graphics.ResetTransform();
				}
			}
		}

		/// <summary>
		/// 获取中心点的坐标
		/// </summary>
		/// <returns></returns>
		// Token: 0x06001EC5 RID: 7877 RVA: 0x00094EF4 File Offset: 0x000930F4
		private OperateResult<Point, int, double> GetCenterPoint()
		{
			OperateResult<Point, int, double> operateResult = new OperateResult<Point, int, double>();
			bool flag = base.Height <= 35;
			OperateResult<Point, int, double> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				bool flag2 = base.Width <= 20;
				if (flag2)
				{
					result = operateResult;
				}
				else
				{
					operateResult.IsSuccess = true;
					bool flag3 = !this.IsBigSemiCircle;
					if (flag3)
					{
						operateResult.Content2 = base.Height - 30;
						bool flag4 = (double)(base.Width - 40) / 2.0 > (double)operateResult.Content2;
						if (flag4)
						{
							operateResult.Content3 = Math.Acos(1.0) * 180.0 / 3.141592653589793;
						}
						else
						{
							operateResult.Content3 = Math.Acos((double)(base.Width - 40) / 2.0 / (double)(base.Height - 30)) * 180.0 / 3.141592653589793;
						}
						operateResult.Content1 = new Point(base.Width / 2, base.Height - 10);
						result = operateResult;
					}
					else
					{
						operateResult.Content2 = (base.Width - 40) / 2;
						bool flag5 = base.Height - 30 < operateResult.Content2;
						if (flag5)
						{
							operateResult.Content2 = base.Height - 30;
							operateResult.Content3 = Math.Acos(1.0) * 180.0 / 3.141592653589793;
							operateResult.Content1 = new Point(base.Width / 2, base.Height - 10);
							result = operateResult;
						}
						else
						{
							int num = base.Height - 30 - operateResult.Content2;
							bool flag6 = num > operateResult.Content2;
							if (flag6)
							{
								num = operateResult.Content2;
							}
							operateResult.Content3 = -Math.Asin((double)num * 1.0 / (double)operateResult.Content2) * 180.0 / 3.141592653589793;
							operateResult.Content1 = new Point(base.Width / 2, operateResult.Content2 + 20);
							result = operateResult;
						}
					}
				}
			}
			return result;
		}

		// Token: 0x06001EC6 RID: 7878 RVA: 0x00095124 File Offset: 0x00093324
		private void ThreadPoolUpdateProgress(object obj)
		{
			try
			{
				int num = (int)obj;
				bool flag = this.value_paint == this.value_current;
				if (!flag)
				{
					double num2 = Math.Abs(this.value_paint - this.value_current) / 10.0;
					bool flag2 = num2 == 0.0;
					if (flag2)
					{
						num2 = 1.0;
					}
					while (this.value_paint != this.value_current)
					{
						Thread.Sleep(17);
						bool flag3 = num != this.m_version;
						if (flag3)
						{
							break;
						}
						this.hybirdLock.Enter();
						bool flag4 = this.value_paint > this.value_current;
						double num4;
						if (flag4)
						{
							double num3 = this.value_paint - this.value_current;
							bool flag5 = num3 > num2;
							if (flag5)
							{
								num3 = num2;
							}
							num4 = this.value_paint - num3;
						}
						else
						{
							double num5 = this.value_current - this.value_paint;
							bool flag6 = num5 > num2;
							if (flag6)
							{
								num5 = num2;
							}
							num4 = this.value_paint + num5;
						}
						this.value_paint = num4;
						this.hybirdLock.Leave();
						bool flag7 = num == this.m_version;
						if (!flag7)
						{
							break;
						}
						bool isHandleCreated = base.IsHandleCreated;
						if (isHandleCreated)
						{
							base.Invoke(this.m_UpdateAction);
						}
					}
				}
			}
			catch (Exception)
			{
			}
		}

		/// <summary>
		/// 获取或设置仪表盘的背景色
		/// </summary>
		// Token: 0x17000655 RID: 1621
		// (get) Token: 0x06001EC7 RID: 7879 RVA: 0x000952B0 File Offset: 0x000934B0
		// (set) Token: 0x06001EC8 RID: 7880 RVA: 0x000952C8 File Offset: 0x000934C8
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置仪表盘的背景色")]
		[DefaultValue(typeof(Color), "DimGray")]
		public Color GaugeBorder
		{
			get
			{
				return this.color_gauge_border;
			}
			set
			{
				Pen pen = this.pen_gauge_border;
				if (pen != null)
				{
					pen.Dispose();
				}
				this.pen_gauge_border = new Pen(value);
				this.color_gauge_border = value;
				base.Invalidate();
			}
		}

		/// <summary>
		/// 获取或设置指针的颜色
		/// </summary>
		// Token: 0x17000656 RID: 1622
		// (get) Token: 0x06001EC9 RID: 7881 RVA: 0x000952F8 File Offset: 0x000934F8
		// (set) Token: 0x06001ECA RID: 7882 RVA: 0x00095310 File Offset: 0x00093510
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置仪表盘指针的颜色")]
		[DefaultValue(typeof(Color), "Tomato")]
		public Color PointerColor
		{
			get
			{
				return this.color_gauge_pointer;
			}
			set
			{
				Brush brush = this.brush_gauge_pointer;
				if (brush != null)
				{
					brush.Dispose();
				}
				this.brush_gauge_pointer = new SolidBrush(value);
				this.color_gauge_pointer = value;
				base.Invalidate();
			}
		}

		/// <summary>
		/// 获取或设置数值的起始值，默认为0
		/// </summary>
		// Token: 0x17000657 RID: 1623
		// (get) Token: 0x06001ECB RID: 7883 RVA: 0x00095340 File Offset: 0x00093540
		// (set) Token: 0x06001ECC RID: 7884 RVA: 0x00095382 File Offset: 0x00093582
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置数值的起始值，默认为0")]
		[DefaultValue(0.0)]
		public double ValueStart
		{
			get
			{
				bool flag = this.value_max <= this.value_start;
				double result;
				if (flag)
				{
					result = this.value_start + 1.0;
				}
				else
				{
					result = this.value_start;
				}
				return result;
			}
			set
			{
				this.value_start = value;
				base.Invalidate();
			}
		}

		/// <summary>
		/// 获取或设置数值的最大值，默认为100
		/// </summary>
		// Token: 0x17000658 RID: 1624
		// (get) Token: 0x06001ECD RID: 7885 RVA: 0x00095394 File Offset: 0x00093594
		// (set) Token: 0x06001ECE RID: 7886 RVA: 0x000953D6 File Offset: 0x000935D6
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置数值的最大值，默认为100")]
		[DefaultValue(100.0)]
		public double ValueMax
		{
			get
			{
				bool flag = this.value_max <= this.value_start;
				double result;
				if (flag)
				{
					result = this.value_start + 1.0;
				}
				else
				{
					result = this.value_max;
				}
				return result;
			}
			set
			{
				this.value_max = value;
				base.Invalidate();
			}
		}

		/// <summary>
		/// 获取或设置数值的当前值，应该处于最小值和最大值之间
		/// </summary>
		// Token: 0x17000659 RID: 1625
		// (get) Token: 0x06001ECF RID: 7887 RVA: 0x000953E8 File Offset: 0x000935E8
		// (set) Token: 0x06001ED0 RID: 7888 RVA: 0x00095400 File Offset: 0x00093600
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置数值的当前值，默认为0")]
		[DefaultValue(0.0)]
		public double Value
		{
			get
			{
				return this.value_current;
			}
			set
			{
				bool flag = this.ValueStart <= value && value <= this.ValueMax;
				if (flag)
				{
					bool flag2 = value != this.value_current;
					if (flag2)
					{
						this.value_current = value;
						int num = Interlocked.Increment(ref this.m_version);
						ThreadPool.QueueUserWorkItem(new WaitCallback(this.ThreadPoolUpdateProgress), num);
					}
				}
			}
		}

		/// <summary>
		/// 获取或设置数值的上限报警值，设置为超过最大值则无上限报警
		/// </summary>
		// Token: 0x1700065A RID: 1626
		// (get) Token: 0x06001ED1 RID: 7889 RVA: 0x0009546C File Offset: 0x0009366C
		// (set) Token: 0x06001ED2 RID: 7890 RVA: 0x00095484 File Offset: 0x00093684
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置数值的上限报警值，设置为超过最大值则无上限报警，默认为80")]
		[DefaultValue(80.0)]
		public double ValueAlarmMax
		{
			get
			{
				return this.value_alarm_max;
			}
			set
			{
				bool flag = this.ValueStart <= value;
				if (flag)
				{
					this.value_alarm_max = value;
					base.Invalidate();
				}
			}
		}

		/// <summary>
		/// 获取或设置数值的下限报警值，设置为超过最大值则无上限报警
		/// </summary>
		// Token: 0x1700065B RID: 1627
		// (get) Token: 0x06001ED3 RID: 7891 RVA: 0x000954B4 File Offset: 0x000936B4
		// (set) Token: 0x06001ED4 RID: 7892 RVA: 0x000954CC File Offset: 0x000936CC
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置数值的下限报警值，设置为小于最小值则无下限报警，默认为20")]
		[DefaultValue(20.0)]
		public double ValueAlarmMin
		{
			get
			{
				return this.value_alarm_min;
			}
			set
			{
				bool flag = value <= this.ValueMax;
				if (flag)
				{
					this.value_alarm_min = value;
					base.Invalidate();
				}
			}
		}

		/// <summary>
		/// 获取或设置仪表盘的分割段数，最小为2，最大1000
		/// </summary>
		// Token: 0x1700065C RID: 1628
		// (get) Token: 0x06001ED5 RID: 7893 RVA: 0x000954FC File Offset: 0x000936FC
		// (set) Token: 0x06001ED6 RID: 7894 RVA: 0x00095514 File Offset: 0x00093714
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置仪表盘的分割段数，最小为2，最大1000")]
		[DefaultValue(10)]
		public int SegmentCount
		{
			get
			{
				return this.segment_count;
			}
			set
			{
				bool flag = value > 1 && value < 1000;
				if (flag)
				{
					this.segment_count = value;
				}
			}
		}

		/// <summary>
		/// 获取或设置仪表盘的单位描述文本
		/// </summary>
		// Token: 0x1700065D RID: 1629
		// (get) Token: 0x06001ED7 RID: 7895 RVA: 0x00095540 File Offset: 0x00093740
		// (set) Token: 0x06001ED8 RID: 7896 RVA: 0x00095558 File Offset: 0x00093758
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置仪表盘的单位描述文本")]
		[DefaultValue("")]
		public string UnitText
		{
			get
			{
				return this.value_unit_text;
			}
			set
			{
				this.value_unit_text = value;
				base.Invalidate();
			}
		}

		/// <summary>
		/// 获取或设置文本是否是指针的下面
		/// </summary>
		// Token: 0x1700065E RID: 1630
		// (get) Token: 0x06001ED9 RID: 7897 RVA: 0x0009556C File Offset: 0x0009376C
		// (set) Token: 0x06001EDA RID: 7898 RVA: 0x00095584 File Offset: 0x00093784
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置文本是否是指针的下面")]
		[DefaultValue(true)]
		public bool IsTextUnderPointer
		{
			get
			{
				return this.text_under_pointer;
			}
			set
			{
				this.text_under_pointer = value;
				base.Invalidate();
			}
		}

		/// <summary>
		/// 通常情况，仪表盘不会大于半个圆，除非本属性设置为 True
		/// </summary>
		// Token: 0x1700065F RID: 1631
		// (get) Token: 0x06001EDB RID: 7899 RVA: 0x00095598 File Offset: 0x00093798
		// (set) Token: 0x06001EDC RID: 7900 RVA: 0x000955B0 File Offset: 0x000937B0
		[Browsable(true)]
		[Category("外观")]
		[Description("通常情况，仪表盘不会大于半个圆，除非本属性设置为 True")]
		[DefaultValue(false)]
		public bool IsBigSemiCircle
		{
			get
			{
				return this.isBigSemiCircle;
			}
			set
			{
				this.isBigSemiCircle = value;
				base.Invalidate();
			}
		}

		/// <summary> 
		/// 清理所有正在使用的资源。
		/// </summary>
		/// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
		// Token: 0x06001EDD RID: 7901 RVA: 0x000955C4 File Offset: 0x000937C4
		protected override void Dispose(bool disposing)
		{
			bool flag = disposing && this.components != null;
			if (flag)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		/// <summary> 
		/// 设计器支持所需的方法 - 不要修改
		/// 使用代码编辑器修改此方法的内容。
		/// </summary>
		// Token: 0x06001EDE RID: 7902 RVA: 0x000955FC File Offset: 0x000937FC
		private void InitializeComponent()
		{
			base.SuspendLayout();
			base.AutoScaleDimensions = new SizeF(7f, 17f);
			base.AutoScaleMode = AutoScaleMode.Font;
			this.BackColor = Color.Transparent;
			this.Font = new Font("微软雅黑", 9f, FontStyle.Regular, GraphicsUnit.Point, 134);
			base.Margin = new Padding(3, 4, 3, 4);
			base.Name = "UserGaugeChart";
			base.Size = new Size(257, 226);
			base.Load += this.UserGaugeChart_Load;
			base.Paint += this.UserGaugeChart_Paint;
			base.ResumeLayout(false);
		}

		// Token: 0x040006E9 RID: 1769
		private Color color_gauge_border = Color.DimGray;

		// Token: 0x040006EA RID: 1770
		private Pen pen_gauge_border = null;

		// Token: 0x040006EB RID: 1771
		private Color color_gauge_pointer = Color.Tomato;

		// Token: 0x040006EC RID: 1772
		private Brush brush_gauge_pointer = null;

		// Token: 0x040006ED RID: 1773
		private double value_start = 0.0;

		// Token: 0x040006EE RID: 1774
		private double value_max = 100.0;

		// Token: 0x040006EF RID: 1775
		private double value_current = 0.0;

		// Token: 0x040006F0 RID: 1776
		private double value_alarm_max = 80.0;

		// Token: 0x040006F1 RID: 1777
		private double value_alarm_min = 20.0;

		// Token: 0x040006F2 RID: 1778
		private Pen pen_gauge_alarm = null;

		// Token: 0x040006F3 RID: 1779
		private int m_version = 0;

		// Token: 0x040006F4 RID: 1780
		private double value_paint = 0.0;

		// Token: 0x040006F5 RID: 1781
		private SimpleHybirdLock hybirdLock;

		// Token: 0x040006F6 RID: 1782
		private Action m_UpdateAction;

		// Token: 0x040006F7 RID: 1783
		private Timer timer_alarm_check;

		// Token: 0x040006F8 RID: 1784
		private bool alarm_check = false;

		// Token: 0x040006F9 RID: 1785
		private int segment_count = 10;

		// Token: 0x040006FA RID: 1786
		private StringFormat centerFormat = null;

		// Token: 0x040006FB RID: 1787
		private string value_unit_text = string.Empty;

		// Token: 0x040006FC RID: 1788
		private bool text_under_pointer = true;

		// Token: 0x040006FD RID: 1789
		private bool isBigSemiCircle = false;

		/// <summary> 
		/// 必需的设计器变量。
		/// </summary>
		// Token: 0x040006FE RID: 1790
		private IContainer components = null;
	}
}
